import { getAreaTree } from '@/api/common/index';

import { FormSchema } from '@/components/Form';
import { getAddressBook, getSalesmanListApi } from '@/api/pubInfo/index';
import { getWarehouseOwnerConditions } from '@/api/warehouse/index';

export const schemas: FormSchema[] = [
  {
    field: 'consigneeWarehouse',
    component: 'ApiSelect',
    label: '提货仓库',
    componentProps: ({ formModel }) => {
      return {
        showSearch: true,
        api: getWarehouseOwnerConditions,
        resultField: 'records',
        optionFilterProp: 'label',
        immediate: false,
        params: { keyword: '', current: 1, size: 100 },
        labelField: 'houseName',
        valueField: 'id',
        onChange: (e, h) => {
          if (h) {
            console.log('-----------------h', formModel, h);
            // console.log('-----------------xxxxxxxxxx', formModel.area1);
            // if (formModel.area1==undefined){
            //   formModel['area1'] = [];
            // }
            // formModel['area1'] = [];
            formModel.consigneeWarehouse = h.label;
            formModel.consigneeName = h.contacts;
            formModel.consigneePhone = h.contactsPhone;
            formModel.consigneeProvince = h.province;
            formModel.consigneeAddress = h.city;
            formModel.consigneeCity = h.area;
            formModel.consigneeArea = h.address;
            formModel.consigneeAreaCode = h.areaCode;
            formModel.area1 = [h.province, h.city, h.area];
            // formModel.area1 = ['广东省', '广州市', '黄埔区'];
          }
        },
      };
    },
    required: true,
  },
  {
    field: 'consigneeName',
    component: 'Input',
    label: '联系人',
    required: true,
    colProps: {
      offset: 2,
    },
  },
  {
    field: 'consigneePhone',
    component: 'Input',
    label: '联系电话',
    required: true,
    colProps: {
      offset: 2,
    },
  },
  {
    component: 'ApiCascader',
    label: '提货地址',
    field: 'area1',
    required: true,
    colProps: {},
    defaultValue: [],
    componentProps: ({ formModel }) => {
      return {
        showSearch: true,
        placeholder: '请选择',
        api: getAreaTree,
        labelField: 'areaName',
        valueField: 'areaCode',
        displayRenderArray: formModel.area1,
        onChange: (e, s, g, q) => {
          if (e) {
            formModel.consigneeAreaCode = e[e.length - 1];
          }
          formModel.area1 = [];
          console.log('提货地址', formModel.area1);

          // 判断省
          if (s) {
            formModel.consigneeProvince = s.label;
            formModel.area1[0] = s.label;
          } else {
            formModel.consigneeProvince = '';
            formModel.area1 = e;
          }
          // 判断市

          if (g) {
            formModel.consigneeCity = g.label;
            formModel.area1[1] = g.label;
          } else {
            formModel.consigneeCity = '';
            formModel.area1 = e;
          }
          // 判断区
          if (q) {
            formModel.consigneeArea = q.label;
            formModel.area1[2] = q.label;

            // formModel.area = s.label + g.label + q.label;
          } else {
            formModel.consigneeArea = '';
            formModel.area1 = e;
          }
        },
      };
    },
  },
  {
    field: 'consigneeAddress',
    component: 'Input',
    label: '详细地址',
    required: true,
    colProps: {
      offset: 2,
    },
    componentProps: ({ formModel }) => {
      return {
        onInput: (e) => {
          console.log(e, e.target.value, formModel);
          // formModel.f6 = formModel.f6 + e.target.value;
        },
      };
    },
  },
  {
    field: 'consigneeProvince',
    component: 'Input',
    show: false,
    label: '省',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'consigneeCity',
    component: 'Input',
    show: false,
    label: '市',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'consigneeArea',
    component: 'Input',
    show: false,
    label: '区',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'consigneeAreaCode',
    component: 'Input',
    show: false,
    label: '编码',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'consigneeProvince',
    component: 'Input',
    show: false,
    label: '省',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'consigneeCity',
    component: 'Input',
    show: false,
    label: '市',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'consigneeArea',
    component: 'Input',
    show: false,
    label: '区',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
];
export const basicInformationSchemas: FormSchema[] = [
  {
    field: 'deliveryCode',
    component: 'Input',
    label: '计划单号',
    required: false,
    propTypes: {
      bool: false,
    },
    colProps: {
      disabled: false,
    },
    componentProps: { disabled: true },
  },
  {
    field: 'createTime',
    component: 'Input',
    label: '创建日期',
    required: false,
    colProps: {
      offset: 2,
    },
    componentProps: { disabled: true },
  },
  {
    field: 'createBy',
    component: 'Input',
    label: '创建人',
    required: false,
    componentProps: { disabled: true },
    colProps: {
      offset: 2,
    },
  },
];
export const customerSchemas: FormSchema[] = [
  {
    field: 'receiverCompany',
    component: 'ApiSelect',
    componentProps: ({ formModel }) => {
      return {
        showSearch: true,
        api: getAddressBook,
        resultField: 'records',
        optionFilterProp: 'label',
        immediate: false,
        params: { configCode: 'eslogis_customer', data: { keyword: '', current: 1, size: 100 } },
        labelField: 'receiverCompany',
        valueField: 'id',
        onChange: (e, h) => {
          if (h) {
            formModel.receiverCompany = h.label;
            formModel.receiverName = h.receiverName;
            formModel.receiverPhone = h.receiverPhone;
            formModel.receiverAddress = h.receiverAddress;
            formModel.consigneeProvince = h.province;
            //  formModel.salesmanName = h.receiverProvince;

            formModel.receiverProvince = h.receiverProvince;
            formModel.receiverCity = h.receiverCity;
            formModel.receiverArea = h.receiverArea;
            formModel.receiverAreaCode = h.areaCode;
            formModel.area = [h.receiverProvince, h.receiverCity, h.receiverArea];
          }
        },
      };
    },
    label: '收货单位',
    required: true,
  },
  {
    field: 'receiverName',
    component: 'Input',
    label: '联系人',

    required: true,
    colProps: {
      offset: 2,
    },
  },
  {
    field: 'receiverPhone',
    component: 'Input',
    label: '联系电话',
    colProps: {
      offset: 2,
    },
    required: true,
  },

  {
    field: 'area',
    component: 'ApiCascader',
    label: '收货地址',
    componentProps: ({ formModel }) => {
      return {
        showSearch: true,
        placeholder: '请选择',
        api: getAreaTree,
        labelField: 'areaName',
        valueField: 'areaCode',
        displayRenderArray: formModel.area,
        // displayRenderArray: formModel.f6,
        onChange: (e, s, g, q) => {
          console.log(e, '---------------', s, g, q);
          if (e) {
            formModel.receiverAreaCode = e[e.length - 1];
          }
          formModel.area = [];

          // 判断省
          if (s) {
            formModel.receiverProvince = s.label;
            formModel.area[0] = s.label;
          } else {
            formModel.receiverProvince = '';
            formModel.receiverAddress = '';
            formModel.area = e;
          }
          // 判断市

          if (g) {
            formModel.receiverCity = g.label;
            formModel.area[1] = g.label;
          } else {
            formModel.receiverProvince = '';
            formModel.receiverAddress = '';
            formModel.area = e;
          }
          // 判断区
          if (q) {
            formModel.receiverArea = q.label;
            // formModel.receiverAddress = s.label + g.label + q.label;
            formModel.area[2] = q.label;
          } else {
            formModel.receiverProvince = '';
            formModel.receiverAddress = '';
            formModel.area = e;
          }
        },
      };
    },
    required: true,
    // colProps: {
    //   offset: 2,
    // },
  },
  {
    field: 'receiverAddress',
    component: 'Input',
    label: '详细地址',
    required: true,
    colProps: {
      offset: 2,
    },
  },
  // {
  //   field: 't1',
  //   component: 'InputTextArea',
  //   label: '完整地址',
  //   required: true,
  //   colProps: {
  //     offset: 2,
  //   },
  // },
  {
    field: 'serialCode',
    component: 'Input',
    label: '合同号',
    required: true,
    colProps: {
      offset: 2,
    },
  },
  {
    field: 'salesmanName',
    component: 'ApiSelect',
    label: '业务员',
    componentProps: ({ formModel }) => {
      return {
        showSearch: true,
        api: getSalesmanListApi,
        resultField: 'records',
        optionFilterProp: 'label',
        immediate: false,
        params: { data: { keyword: '', current: 1, size: 100 } },
        labelField: 'salesmanName',
        valueField: 'salesmanName',
        onChange: (e, h) => {
          console.log('e----------------', e);

          if (h) {
            console.log(formModel, e, h);

            // formModel.consigneeWarehouse = h.consigneeWarehouse;
            // formModel.consigneeName = h.consigneeName;
            // formModel.consigneePhone = h.consigneePhone;
            // formModel.consigneeAddress = h.consigneeAddress;
            // formModel.area1 = [h.consigneeProvince, h.consigneeCity, h.consigneeArea];
          }
        },
        onblur: (e, h) => {
          console.log(e, h);
        },
      };
    },
    required: true,
  },
  {
    field: 'receiverProvince',
    component: 'Input',
    show: false,
    label: '省',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'receiverCity',
    component: 'Input',
    show: false,
    label: '市',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'receiverArea',
    component: 'Input',
    show: false,
    label: '区',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: true,
  },
  {
    field: 'receiverAreaCode',
    component: 'Input',
    show: false,
    label: '编码',
    colProps: {
      span: 8,
      offset: 2,
    },
    required: false,
  },
];

export const requirementsSchemas: FormSchema[] = [
  {
    field: 'receiverRemark',
    component: 'InputTextArea',
    label: '',
    required: true,
    colProps: {
      span: 24,
    },
  },
];
